﻿using huqiang.Core.HGUI;
using huqiang.Data;
using huqiang.UIEvent;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace huqiang.UIComposite
{
    public class AppendText:Composite
    {
        //UISlider _sliderv;
        UISlider _sliderV;
        public UISlider SliderV { get => _sliderV;
            set { 
                _sliderV = value;
                if (_sliderV != null) 
                    SetSliderVEvent();
            } 
        }
        /// <summary>
        /// 主体事件
        /// </summary>
        public UserEvent eventCall;
        TextBox mainText;
        int _maxLine = 2000;
        public int MaxLine { 
            get => _maxLine; 
            set {
                _maxLine = value; 
            } 
        }
        int sLine;
        int showLine = 10;
        public int CurrentLine { get; }
        List<string> buf;
        public override void Initial(FakeStruct mod, UIElement element, UIInitializer initializer)
        {
            base.Initial(mod, element, initializer);
            eventCall = element.RegEvent<UserEvent>();
            eventCall.AutoColor = false;
            var s= element.Find("SliderV");
            if (s != null)
                _sliderV = s.composite as UISlider;
            mainText = element.Find("TextBox") as TextBox;
            
            buf = new List<string>();
            buf.Add("");
            sLine = 0;
            InitialEvent();
        }
        void InitialEvent()
        {
            //eventCall.Click = (o, e) => { 
            
            //};
            eventCall.MouseWheel = (o, e) => {
                if (e.MouseWheelDelta < 0)
                    sLine++;
                else sLine--;
                Show();
            };
            if (_sliderV != null)
                SetSliderVEvent();
            Enity.SizeChanged = (o) => { Show(); };
        }
        void SetSliderVEvent()
        {
            _sliderV.OnValueChanged = (o) => {
                int showLine = (int)(Enity.m_sizeDelta.y / HTextGenerator.LineSpacing / mainText.FontSize);
                float s =(1- _sliderV.Percentage )* (_maxLine - showLine);
                sLine = (int)s;
                Show();
            };
        }
        public void AddString(string str)
        {
            int c = buf.Count - 1;
            buf[c] = buf[c] + '\n';
            string[] ss = str.Split('\n');
            for(int i=0;i<ss.Length;i++)
            {
                if (ss[i] != "")
                    buf.Add(ss[i] + '\n');
            }
            buf.Add("");
            sLine = buf.Count - showLine;
            Show();
        }
        public void Write(string str)
        {
            int c = buf.Count - 1;
            buf[c] = buf[c] + str;
            Show();
        }
        public void WriteLine(string str)
        {
            int c = buf.Count - 1;
            buf[c] = buf[c] + str+'\n';
            buf.Add("");
            if (sLine + showLine <= c )
                sLine++;
            Show();
        }
        public void End()
        {
            sLine = buf.Count - showLine;
            Show();
        }
        /// <summary>
        /// 撤销字符
        /// </summary>
        /// <param name="count">字符数量</param>
        public void Revoke(int count)
        {
            int c = buf.Count - 1;
            string str = buf[c];
            int len = str.Length;
            if (len <= count)
                buf[c] = "";
            else buf[c] = str.Remove(len - count,count);
            Show();
        }
        /// <summary>
        /// 撤销行
        /// </summary>
        /// <param name="count">行数</param>
        public void RevokeLine(int count)
        {
            int c = buf.Count;
            int index = c - count;
            if(index<=0)
            {
                buf.Clear();
                buf.Add("");
                Show();
                return;
            }
            buf.RemoveRange(index,count);
            Show();
        }
        public void Clear()
        {
            buf.Clear();
            buf.Add("");
            sLine = 0;
            Show();
        }
        StringBuilder buffer;
        void Show()
        {
            int c = buf.Count - _maxLine;
            if (c > 0)
                buf.RemoveRange(0, c);
            showLine = (int)(Enity.m_sizeDelta.y/HTextGenerator.LineSpacing / mainText.FontSize);
            if (sLine + showLine > _maxLine + 1)
            {
                sLine = _maxLine + 1 - showLine;
            }else if(sLine<0)
            {
                sLine = 0;
            }
            if (buffer == null)
                buffer = new StringBuilder();
            else buffer.Clear();
            int s = sLine;
            c = buf.Count;
            for(int i=0;i<showLine;i++)
            {
                if (s < c)
                    buffer.Append(buf[s]);
                else break;
                s++;
            }
            mainText.Text = buffer.ToString();
            if (_sliderV != null)
            {
                float l = sLine;
                float a = _maxLine - showLine;
                _sliderV.Percentage =1 - l / a;
            }
        }
    }
}
